package com.liuzhilong.infrastructure.basic.jdk.thread;

import com.sun.org.apache.regexp.internal.RE;
import lombok.SneakyThrows;

import java.util.concurrent.*;

public class CompletableFutureUse {
    public static ExecutorService RECEIVER_POOL;//队列满了的处理策略
    static {
        RECEIVER_POOL = new ThreadPoolExecutor(5, //常驻5个线程
                100,//最大扩展到100个线程
                0,//非核心线程的空闲时间
                TimeUnit.SECONDS,//
                new ArrayBlockingQueue<Runnable>(512), // 使用有界队列，避免OOM
                new ThreadPoolExecutor.AbortPolicy());
        System.out.println("开启线程池");

    }

    @SneakyThrows
    public static void main(String[] args) {
        CompletableFuture<String> string1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("string1 start");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "1";
        }, RECEIVER_POOL);
        CompletableFuture<String> string2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("string2 start");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "2";
        }, RECEIVER_POOL);
        string1.thenAccept(v1 -> System.out.println("我收到了" + v1));
        CompletableFuture.allOf(string1, string1).join();
        System.out.println(string1.get()+string2.get());
        RECEIVER_POOL.shutdown();
    }

}
